package org.easy.alioss;


import java.io.InputStream;

/**
 * @author: Li chaojie
 * @Date: 2022/7/9
 * @Description:
 */
public class AliOssTemplate {
    public AliOssTemplate(OssBaseProperties ossBaseProperties, AliOssAdapter ossClientAdapter) {
        this.ossBaseProperties = ossBaseProperties;
        this.ossClientAdapter = ossClientAdapter;
    }

    private final OssBaseProperties ossBaseProperties;

    private final AliOssAdapter ossClientAdapter;

    /**
     * 上传文件
     *
     * @param inputStream
     * @param ossKey
     * @return
     */
    public FileOutDto upload(InputStream inputStream, String ossKey) {
        return ossClientAdapter.upload(ossBaseProperties, ossBaseProperties.getEndpoint(), ossBaseProperties.getBucket(), ossKey, inputStream);
    }

    /**
     * 上传文件
     * @param inputStream
     * @param bucket
     * @param ossKey
     * @return
     */
    public FileOutDto upload(InputStream inputStream, String bucket, String ossKey) {
        return ossClientAdapter.upload(ossBaseProperties, ossBaseProperties.getEndpoint(), bucket, ossKey, inputStream);
    }

    /**
     * 上传文件
     *
     * @param inputStream
     * @param endpoint
     * @param bucket
     * @param ossKey
     * @return
     */
    public FileOutDto upload(InputStream inputStream, String endpoint, String bucket, String ossKey) {
        return ossClientAdapter.upload(ossBaseProperties, endpoint, bucket, ossKey, inputStream);
    }


    /**
     * 获得公开桶的永久链接
     *
     * @param bucket
     * @param ossKey
     * @return
     */
    public String getPublicUrl(String bucket, String ossKey) {
        return ossClientAdapter.getPublicUrl(ossBaseProperties, bucket, ossKey);
    }

    /**
     * 获得预签链接
     *
     * @param bucket
     * @param ossKey
     * @param expires 过期时间 单位秒  最大7 * 24 * 3600秒
     * @return
     */
    public String getPresignedUrl(String bucket, String ossKey, Integer expires) {
        return ossClientAdapter.getPresignedUrl(ossBaseProperties, ossBaseProperties.getEndpoint(), bucket, ossKey, expires);
    }


    /**
     * 对象是否存在
     *
     * @param bucket
     * @param ossKey
     * @return
     */
    public Boolean isExitsObject(String bucket, String ossKey) {
        return ossClientAdapter.isExitsObject(ossBaseProperties, bucket, ossKey);
    }

    /**
     * 文件下载
     *
     * @param bucket
     * @param ossKey
     * @return 输出流  注意！！ 需要手动关闭
     */
    public InputStream downloadFile(String bucket, String ossKey) {
        return ossClientAdapter.downloadFile(ossBaseProperties, bucket, ossKey);
    }
}
